package tech.aistar.controller.registerAndLogin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.User.CodeStatus;
import tech.aistar.entity.User.UserResult;
import tech.aistar.entity.User.ValidGroup1;

import tech.aistar.entity.authordis.UserTable;
import tech.aistar.service.registerAndLogin.IUserLoginService;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2019/9/28.
 */
@Controller
@RequestMapping("/userlogin")
public class login extends HttpServlet {
    @Autowired
    private IUserLoginService userLoginService;


    @RequestMapping("/login")
    public String returnLogin() {
        return "register/login";
    }

    @RequestMapping("/index")
    public String returnIndex() {



        return "index";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest req){
        HttpSession session = req.getSession();
        session.invalidate();
        return "index";
    }



    @PostMapping("/checkUsers")
    public String checkUser(UserTable userTable,HttpServletRequest req) {
        HttpSession session = req.getSession();
        UserTable u = userLoginService.findByPhone(userTable.getPhone(), userTable.getPassword());

        if (null == u) {
            return "redirect:login?error=0";
        }
        session.setAttribute("userId", u);
        System.out.println(session.getAttribute("userId"));
        UserResult userResult = new UserResult(CodeStatus.ERROR, true, "");
        return "redirect:/userlogin/index";
    }
}
